package org.jeecg.modules.lab.entrust.service.impl;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.oss.ServiceException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.TokenServess;
import org.jeecg.modules.lab.entrust.entity.LabWscientific;
import org.jeecg.modules.lab.entrust.mapper.LabWentrustMapper;
import org.jeecg.modules.lab.entrust.mapper.LabWscientificMapper;
import org.jeecg.modules.lab.entrust.service.ILabWentrustService;
import org.jeecg.modules.lab.entrust.service.ILabWscientificService;
import org.jeecg.modules.lab.entrust.vo.LabWscientificItemPage;
import org.jeecg.modules.lab.entrust.vo.LabWscientificPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.net.MalformedURLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 科研项目
 * @Author: jeecg-boot
 * @Date: 2023-11-17
 * @Version: V1.0
 */
@Service
public class LabWscientificServiceImpl extends ServiceImpl<LabWscientificMapper, LabWscientific> implements ILabWscientificService {

    @Autowired
    private LabWscientificMapper labWscientificMapper;
    @Autowired
    private LabWentrustMapper labEntrustMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void syncKyProject(String kyStr) throws MalformedURLException {
        TokenServess tokenServess = new TokenServess();
        String jsonString = tokenServess.scientificResearch(kyStr);

        /*String jsonString = "{\n" +
                "    \"code\": 200,\n" +
                "    \"message\": \"获取科研项目成功!\",\n" +
                "    \"data\": \"[{\\\"ProjectId\\\":3,\\\"ProjectCode\\\":\\\"地方\\\",\\\"ProjectName\\\":\\\"会更好\\\",\\\"ProjectContent\\\":\\\"共和国\\\",\\\"ProjectLevel\\\":\\\"group\\\",\\\"ProjectAttribute\\\":\\\"project\\\",\\\"ProjectType\\\":\\\"major\\\",\\\"ProfessionType\\\":\\\"cljs\\\",\\\"InchargeUnit\\\":8,\\\"ProjectManager\\\":449,\\\"ProjectMembers\\\":\\\"\\\",\\\"StartDate\\\":\\\"2023-11-10T00:00:00\\\",\\\"EndDate\\\":\\\"2023-11-14T00:00:00\\\",\\\"StudyDomain\\\":\\\"\\\",\\\"Kpi\\\":\\\"\\\",\\\"IpOutcome\\\":\\\"\\\",\\\"Remark\\\":\\\"\\\",\\\"Status\\\":\\\"active\\\",\\\"IsDeleted\\\":0,\\\"CreateUser\\\":38,\\\"CreateTime\\\":\\\"2023-11-10T14:15:32.1\\\"},{\\\"ProjectId\\\":33,\\\"ProjectCode\\\":\\\"1\\\",\\\"ProjectName\\\":\\\"1111\\\",\\\"ParentId\\\":1,\\\"ProjectContent\\\":\\\"111111\\\",\\\"ProjectLevel\\\":\\\"national\\\",\\\"ProjectAttribute\\\":\\\"project\\\",\\\"ProjectType\\\":\\\"research\\\",\\\"ProfessionType\\\":\\\"cljs\\\",\\\"InchargeUnit\\\":5,\\\"ProjectManager\\\":449,\\\"ProjectMembers\\\":\\\"\\\",\\\"StartDate\\\":\\\"2023-11-11T00:00:00\\\",\\\"EndDate\\\":\\\"2023-11-15T00:00:00\\\",\\\"StudyDomain\\\":\\\"\\\",\\\"Kpi\\\":\\\"\\\",\\\"IpOutcome\\\":\\\"\\\",\\\"Remark\\\":\\\"\\\",\\\"Status\\\":\\\"active\\\",\\\"IsDeleted\\\":0,\\\"CreateUser\\\":36,\\\"CreateTime\\\":\\\"2023-11-11T11:09:03.223\\\"}]\"\n" +
                "}";*/

        // 解析JSON字符串
        JSONObject jsonObject = JSON.parseObject(jsonString);
        // 获取code属性的值
        int code = jsonObject.getIntValue("code");
        if (code == 200) {
            QueryWrapper<LabWscientific> queryWrapper = new QueryWrapper<>();
            //查询数据库已有的科研数据
            List<LabWscientific> labWscientificList = labWscientificMapper.selectList(queryWrapper);
            String dataString = jsonObject.getString("data");
            JSONArray dataArray = JSON.parseArray(dataString);

            for (int i = 0; i < dataArray.size(); i++) {
                JSONObject projectObject = dataArray.getJSONObject(i);
                //科研项目信息
                String projectId = projectObject.getString("ProjectId");//科研项目id
                String projectCode = projectObject.getString("ProjectCode");//科研项目编码
                String projectName = projectObject.getString("ProjectName");//科研项目名称
                String projectManagerId = projectObject.getString("ProjectManager");//项目经理id
                //实验方案信息
                String planId = projectObject.getString("PlanId");//实验方案id
                String planTitle = projectObject.getString("PlanTitle");//实验方案名称
                String planContent = projectObject.getString("PlanContent");//实验方案内容
                String planUser = projectObject.getString("PlanUser");//实验方案人员

                String status = projectObject.getString("Status");//科研实验状态
                String remark = projectObject.getString("Remark");//备注

                String projectManager = "";//项目经理
                if (projectManagerId != null || !"".equals(projectManagerId)) {
                    LoginUser loginUser = labEntrustMapper.byLoginUser(Integer.parseInt(projectManagerId));
                    projectManager = loginUser.getRealname();
                }
                String PlanUsers = "";//实验人员
                if (planUser != null || !"".equals(planUser)) {
                    String[] parts = planUser.split(",");
                    for (String part : parts) {
                        LoginUser loginUser = labEntrustMapper.byLoginUser(Integer.parseInt(part));
                        if ("".equals(PlanUsers)) {
                            PlanUsers = loginUser.getRealname();
                        } else {
                            PlanUsers += "," + loginUser.getRealname();
                        }
                    }
                }

                Boolean bool = true;
                for (int z = 0; z < labWscientificList.size(); z++) {
                    LabWscientific wscientific = labWscientificList.get(z);
                    String plan_Id = wscientific.getPlanId();
                    if (planId.equals(plan_Id)) {
                        wscientific.setKyCode(projectCode);
                        wscientific.setKyName(projectName);
                        wscientific.setKyContent(planContent);
                        wscientific.setKyManager(projectManager);
                        wscientific.setKyManagerId(projectManagerId);
                        wscientific.setPlanTitle(planTitle);
                        wscientific.setPlanContent(planContent);
                        wscientific.setPlanUsers(PlanUsers);
                        wscientific.setPlanUsersId(planUser);
                        wscientific.setStatus(status);
                        wscientific.setRemark(remark);
                        wscientific.setSynFlag("1");
                        labWscientificMapper.updateById(wscientific);
                        bool = false;
                        break;
                    }
                }
                if (bool) {
                    LabWscientific wscientific = new LabWscientific();
                    wscientific.setKyId(projectId);
                    wscientific.setKyCode(projectCode);
                    wscientific.setKyName(projectName);
                    wscientific.setKyContent(planContent);
                    wscientific.setKyManager(projectManager);
                    wscientific.setKyManagerId(projectManagerId);
                    wscientific.setPlanId(planId);
                    wscientific.setPlanTitle(planTitle);
                    wscientific.setPlanContent(planContent);
                    wscientific.setPlanUsers(PlanUsers);
                    wscientific.setPlanUsersId(planUser);
                    wscientific.setStatus(status);
                    wscientific.setRemark(remark);
                    wscientific.setSynFlag("0");
                    labWscientificMapper.insert(wscientific);
                }
            }
        } else {
            throw new ServiceException("同步科研数据失败！");
        }
    }

    @Override
    public List<LabWscientificPage> getWKyCount(String year, String kyId, String planId) {
        return labWscientificMapper.getWKyCount(year, kyId, planId);
    }

    @Override
    public List<LabWscientificItemPage> getLabWKyAnalysis(String year, String kyId, String planId) {
        return labWscientificMapper.getLabWKyAnalysis(year, kyId, planId);
    }


}
